<?php
 
defined("_VALID_ACCESS") || die('Direct access forbidden'); // - security feature
 
class SCTV_Maintenance extends Module { // - notice how the class name represents its path
 	
  public function body() { // - modules main code
 		$this->rb = $this->init_module('Utils/RecordBrowser','SCTV_Maintenance','SCTV_Maintenance');
		$this->rb->set_defaults(SCTV_MaintenanceCommon::defaults());
		$this->rb->set_default_order(array('status'=>'ASC', 'requirement_date'=>'DESC')); 
		$this->rb->set_header_properties(array(
			'phone'=>array ('width'=> '80px'),
			'status' => array ('width'=> '80px'),
			'code'=>array('width'=>'60px')
		));
						
    	$this->display_module($this->rb);
		
		eval_js('var jump_to_record_input=$("jump_to_record_input");jump_to_record_input.style.display = "";jump_to_record_input.style.width = "150px";document.getElementById("jump_to_record_input").setAttribute("placeholder","Tìm theo mã lắp đặt");');
    }
	
	public function waiting_for_accountant_support()
	{
		Module::static_set_module_variable('SCTV_Maintenance', 'custom_view', accountant_view);
		$this->waiting_for_support(2);
		Module::static_unset_module_variable('SCTV_Maintenance', 'custom_view');
	}
	public function waiting_for_technical_support()
	{
		Module::static_set_module_variable('SCTV_Maintenance', 'custom_view', technical_view);
		$this->waiting_for_support(1);
		Module::static_unset_module_variable('SCTV_Maintenance', 'custom_view');
	}
	public function waiting_for_business_support()
	{
		Module::static_set_module_variable('SCTV_Maintenance', 'custom_view', business_view);
		$this->waiting_for_support(0);
		Module::static_unset_module_variable('SCTV_Maintenance', 'custom_view');
	}
	
	public function your_incompleted_maintenance()
	{
		$my_contact =  CRM_ContactsCommon::get_my_record();	
		Module::static_set_module_variable('SCTV_Maintenance', 'custom_view', counselor_view);		
		$this->waiting_for_support(-1, $my_contact['id'], $my_contact['company_name'] == 11);
		Module::static_unset_module_variable('SCTV_Maintenance', 'custom_view');
	}
	
	private function waiting_for_support($todo, $counselor = null, $show_callcenter = false)
	{
        if($todo == -1){
            $crits = array('review'=>0, '!lastest_update_day'=>null, '!status'=>0);
        }
        else{
            // Lay cac status ma techincal lam: 1
            $rs = Utils_RecordBrowserCommon::get_records('SCTV_StatusTodo', array('todo'=>$todo));
            $statuses = array();
            foreach($rs as $key=>$value)
            {
                $statuses[]=$value['status'];
            }

            $crits = array('status'=>$statuses);
        }
		
		$this->rb = $this->init_module('Utils/RecordBrowser','SCTV_Maintenance','SCTV_Maintenance');
        eval_js('jQuery(".record_browser_add_new").attr("style","display:none");');
		$this->rb->disable_actions();
		$this->rb->disable_pdf();
		$this->rb->disable_export();
		$this->rb->disable_filters();
		$this->rb->set_header_properties(array(
                'customer_name'=>array('width'=>9),
                'address'=>array('width'=>5),
                'phone'=>array('width'=>6),
                'maintenance_code'=>array('width'=>5),
                'status'=>array('width'=>6),
				'lastest_update_day'=>array('width'=>8)
        ));
		$columns = array(
            'lastest_update_day'=>true,
			'review'=>$todo == -1,
            'delivery_date'=>false,
            'note'=>false,
			'customer_code'=>false,
			'charge'=>false
		);

		if($counselor != null)
		{
            if($show_callcenter)
            {
			    $crits['counselor'] = array($counselor, 118);
                $columns['counselor']=true;
            }
            else
            {
                $crits['counselor'] = $counselor;
                $columns['counselor']=false;
            }
		}
		
		$print_href = 'href="modules/SCTV/Maintenance/excel.php?'.http_build_query(array('cid'=>CID, 'todo'=>$todo, 'counselor'=>$counselor)).'" target="_blank"';
		Base_ActionBarCommon::add('export', __('Export'), $print_href);			
		
    	$this->display_module(
    		$this->rb,
    		array(    	    			     			 					    
    			$crits,
    			$columns,
    			array('lastest_update_day'=>'desc')
			),
			'show_data'
		);
		
	}
	
	public function export($todo, $counselor = null)
	{
		require_once 'modules/Libs/PHPExcel/lib/PHPExcel.php';
		require_once 'modules/Libs/PHPExcel/lib/PHPExcel/Writer/Excel5.php';
		
		$objPHPExcel = new PHPExcel();
		
		$row = 1;$column = 0;
		$sheet = $objPHPExcel->getActiveSheet();
		
		$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
		
		header('Content-Encoding: UTF-8');
		header('Content-type: text/xls; charset=UTF-8');
		header('Content-disposition: attachement; filename="'. $report .date('Y_m_d__H_i_s').'.xls"');
		$objWriter->save('php://output');
	}
	
	public function caption(){
		if (isset($this->rb)) return $this->rb->caption();
	}
	
	public function maintenance_delivery_date_history_addon($arg){
		$rb = $this->init_module('Utils/RecordBrowser','SCTV_MaintenanceDeliveryDateHistory');		
		$DeliveryDateHistory = array(array('maintenance'=>$arg['id']), array(), array());
		$this->display_module($rb,$DeliveryDateHistory,'show_data');
	}
	
	public function maintenance_status_history_addon($arg){
		$rb = $this->init_module('Utils/RecordBrowser','SCTV_MaintenanceStatusHistory');		
		$Status = array(array('maintenance'=>$arg['id']), array(), array());
		$this->display_module($rb,$Status,'show_data');
	}
}
 
?>